﻿#region Using Statements

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

#endregion

#region Disclaimer

/*
 
* Copyright (c) 2011, Michael T. Joiner All rights reserved.

* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the 
* documentation and/or other materials provided with the distribution.

* Neither the name of SagaEngine nor the names of its contributors may be used to endorse or promote products derived from this software 
* without specific prior written permission.

* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
* SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 */

#endregion

namespace SagaEngine
{
    public abstract class SagaGame : IGameComponent, IUpdateable, IDrawable,  IDisposable
    {
        #region Fields

        private bool _IsInitialized = false;

        private Game _Game;
        private GraphicsDeviceManager _GraphicsDeviceManager;

        private event EventHandler<EventArgs> _EnableChanged;
        private event EventHandler<EventArgs> _UpdateOrderChanged;
        private event EventHandler<EventArgs> _DrawOrderChanged;
        private event EventHandler<EventArgs> _VisibleChanged;

        private GameOptions _GameOptions;

        #endregion

        #region Properties

        #region Interface Members

        bool IUpdateable.Enabled
        {
            get { return true; }
        }

        bool IDrawable.Visible
        {
            get { return true; }
        }

        int IUpdateable.UpdateOrder
        {
            get { return 1; }
        }

        int IDrawable.DrawOrder
        {
            get { return 1; }
        }

        event EventHandler<EventArgs> IUpdateable.EnabledChanged
        {
            add { _EnableChanged += value; }
            remove { _EnableChanged -= value; }
        }

        event EventHandler<EventArgs> IUpdateable.UpdateOrderChanged
        {
            add { _UpdateOrderChanged += value; }
            remove { _UpdateOrderChanged -= value; }
        }

        event EventHandler<EventArgs> IDrawable.DrawOrderChanged
        {
            add { _DrawOrderChanged += value; }
            remove { _DrawOrderChanged -= value; }
        }

        event EventHandler<EventArgs> IDrawable.VisibleChanged
        {
            add { _VisibleChanged += value; }
            remove { _VisibleChanged -= value; }
        }

        #endregion

        /// <summary>
        /// Gets the Graphics Device used by this game.
        /// </summary>
        public GraphicsDevice GraphicsDevice
        {
            get { return _GraphicsDeviceManager.GraphicsDevice; }
        }

        #endregion

        /// <summary>
        /// Constructs a new instance of this object.
        /// </summary>
        public SagaGame()
        {
            _Game = new Game();
            _Game.Components.Add(this);
        }

        public void Run() 
        {
            if (_GraphicsDeviceManager == null)
            {
                _GraphicsDeviceManager = new GraphicsDeviceManager(_Game);
                _Game.Run();
            }
        }

        #region Interface Members

        void IGameComponent.Initialize() { }

        void IUpdateable.Update(GameTime gameTime) 
        {
            if (!_IsInitialized)
            {
                // Initialize the Main Game Component.
                Initialize(new GameOptions());

                // wait for the next update call
                return;
            }

            Update(gameTime); 
        }

        void IDrawable.Draw(GameTime gameTime) { Draw(gameTime); }

        void IDisposable.Dispose() { }

        #endregion

        protected virtual void Initialize(GameOptions options) 
        {
            /// Do Initialization on GameOptions.

            if (options == null)
                options = new GameOptions();

            _GameOptions = options;

            _Game.IsFixedTimeStep = false;

            _GraphicsDeviceManager.SynchronizeWithVerticalRetrace = _GameOptions.EnableVsync;
            _GraphicsDeviceManager.PreferredBackBufferHeight = _GameOptions.GameWindowHeight;
            _GraphicsDeviceManager.PreferredBackBufferWidth = _GameOptions.GameWindowWidth;
            _GraphicsDeviceManager.IsFullScreen = _GameOptions.IsFullScreen;

            _GraphicsDeviceManager.ApplyChanges();
            
            _IsInitialized = true;
        }

        protected virtual void Update(GameTime gameTime) { }

        protected virtual void Draw(GameTime gameTime) 
        {
            GraphicsDevice.Clear(_GameOptions.DefaultBackbufferColor);
        }
    }
}
